package com.dididoctor.patient.Activity.Usercentre.Record.Cases.CasesDetail;

import android.content.Context;

import com.dididoctor.patient.Activity.ConstantValue;
import com.dididoctor.patient.Bean.Token;
import com.dididoctor.patient.Http.MyHttpResponseHandler;
import com.dididoctor.patient.MV.BasePresenter;
import com.dididoctor.patient.MV.BusinessClient;
import com.dididoctor.patient.MV.Response;
import com.dididoctor.patient.Utils.Util;
import com.loopj.android.http.RequestParams;

import org.apache.http.Header;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @version 2.0
 * @类描述：
 * @项目名称：ddgh
 * @类名称： CasesDetailPresenter
 * @包名称： com.dididoctor.patient.Activity.Usercentre.Record.Cases.CasesDetail
 * @创建人： zhanjunjie
 * @创建时间：2016/4/15 10:47
 * @修改人：
 * @修改时间：2016/4/15 10:47
 * @修改备注： new parent class(用一句话描述修改了什么)
 */
public class CasesDetailPresenter extends BasePresenter {

    private CasesDetailView view;

    public CasesDetailPresenter(Context context, CasesDetailView view) {
        super(context, view);
        this.view = view;
    }

    public void getcasesdetail(String recId) {
        RequestParams params = new RequestParams();
        params.put("userId", Token.getId());
        params.put("token", Token.getToken());
        params.put("recId", recId);
        BusinessClient.post(ConstantValue.medicaldetail, params, new MyHttpResponseHandler() {

            private List<Map<String, String>> quslistsmaps = new ArrayList<Map<String, String>>();
            private List<Map<String, String>> advicesmaps = new ArrayList<Map<String, String>>();
            private List<Quslist> quslists = new ArrayList<Quslist>();
            private List<String> strings = new ArrayList<String>();
            private List<Advice> advices = new ArrayList<Advice>();
            private Impress impress = new Impress();

            @Override
            public void onMyResponse(Response response) {
                if (response.isSucceed()) {
                    CasesDetail casesDetail = new CasesDetail();
                    casesDetail.setRecId(Util.toString(response.getString("recId")));
                    casesDetail.setHosName(Util.toString(response.getString("hosName")));
                    casesDetail.setClinicDate(Util.toString(response.getString("clinicDate")));
                    casesDetail.setDctName(Util.toString(response.getString("dctName")));
                    casesDetail.setDescr(Util.toString(response.getString("descr")));
                    casesDetail.setResult(Util.toString(response.getString("result")));
                    casesDetail.setHosId(Util.toString(response.getString("hosId")));
                    //图片
                    final Object[] piclists = response.getArgs("piclist");
                    for (int i = 0; i < piclists.length; i++) {
                        strings.add(Util.toString(piclists[i]));
                    }
                    //问诊表
                    quslistsmaps = response.getListData("quslist");
                    for (Map<String, String> map : quslistsmaps) {
                        Quslist quslist = new Quslist();
                        quslist.setContent(Util.toString(map.get("content")));
                        quslist.setAnswer(Util.toString(map.get("answer")));
                        quslist.setQusId(Util.toString(map.get("qusId")));
                        quslist.setType(Util.toString(map.get("type")));
                        quslists.add(quslist);
                    }
                    //医生建议
                    advicesmaps = response.getListData("advices");
                    for (Map<String, String> map : advicesmaps) {
                        Advice advice = new Advice();
                        advice.setType(Util.toString(map.get("type")));
                        advice.setContent(Util.toString(map.get("content")));
                        advices.add(advice);
                    }
                    //评价
                    Map<String, Object> impressmap = response.getMap("impress");
                    impress.setPoint(Util.toString(impressmap.get("point")));
                    impress.setRemark(Util.toString(impressmap.get("remark")));


                    view.getcasesdetail(casesDetail, quslists, advices, impress);
                } else {
                    view.showToastMessage(response.getMsg());
                    view.getcasesdetailfail();
                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                super.onFailure(i, headers, bytes, throwable);
                view.getcasesdetailfail();
            }
        });
    }
}
